
/*
 * Project miaw : mi_rtti.h
 * Created: 2022/3/11
 * This program is unlicensed. See LICENSE for more.
 */


#ifndef MIAW_MI_RTTI_H
#define MIAW_MI_RTTI_H

void initializeRTTI () ;

class Class {
public:
    std::vector<const Class *> m_fathers;
    Class (const std::string & name, const std::string & desc, std::vector<std::string> fathers) ;
    bool isChildOf (const Class * c) const ;
    const Class * getOneFather () const ;
    inline std::string getName () const {return m_name;}
    std::vector<const Class *> getSubclasses () const ;
    inline std::string getDescription () const {return m_description;}
protected:
    std::string m_name;
    std::string m_description;
};

const Class * findClass (const std::string & s) ;

#define DECLARE_CLASS() \
static Class *m_class;  \
public: \
virtual const Class * getClass() const override; \
static const Class * theClass ()

/// \param name Class name, e.g. MaterialResource
/// \param ... Class fathers, e.g. "Object", "Referencable"
#define IMPLEMENT_CLASS(name, desc, ...) \
Class *name::m_class = new Class(#name, desc, {#__VA_ARGS__}); \
const Class *name::getClass () const { \
return m_class;\
}                             \
const Class *name::theClass () {         \
return m_class;               \
}

#define INSTANCE_OF(obj, c) (obj.getClass() == c::theClass())
#define INSTANCEP_OF(objp, c) (objp->getClass() == c::theClass())

/// \brief 对象基类
/// \details 在整个程序之中，该基类将维护一层纤薄的RTTI（运行时类型描述）层
/// 对象的创建与删除应该是线程安全的，并且它们的创建和删除往往会消耗大量时间
/// 因此在高性能和频繁（i.e.一帧内上十次的）运算中，任何对象都不应被创建或者删除。
class Object {
    static Class *m_class;
public:
    virtual const Class * getClass() const;
    static const Class * theClass ();
public:
    virtual std::string toString () const ;
    inline virtual ~Object () = default;
};


#endif //MIAW_MI_RTTI_H
